// components/search/search.js
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    keyword: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    _ip: "http://www.mmdccj.top:9003",
    item: 0,
    pageInfo: null,
    currentPage: 1, // 当前页
    pageSize: 10, // 页面大小
    oldKeyword: null, // 上一次搜索的关键字
    keyword: null, // 搜索关键字
    loading: false, // 是否正在加载
    singleList: null,
    singerList: null,
    firstLoad: true,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    search: function () {
      if (this.data.keyword == '') return
      if (this.data.firstLoad) {
        this.setData({
          firstLoad: false,
        })
      } else {
        this.setData({
          currentPage: this.data.currentPage + 1
        })
      }
      const item = this.data.item
      let reqUrl = this.data._ip + '/search/'
      wx.showLoading({
        title: '加载中',
      })
      if (item == 0) reqUrl += 'music'
      if (item == 1) reqUrl += 'singer'
      if (item == 2) reqUrl += 'CD'
      if (item == 3) reqUrl += 'musicList'
      if (item == 4) reqUrl += 'user'
      console.log('请求地址: ' + reqUrl)
      wx.request({
        url: reqUrl,
        method: 'GET',
        header: {
          'Cookie': `JSESSIONID=${wx.getStorageSync('cookie')}`
        },
        data: {
          keyword: this.data.keyword,
          currentPage: this.data.currentPage,
          pageSize: this.data.pageSize
        },
        dataType: JSON,
        success: res => {
          const data = JSON.parse(res.data)
          // 搜索音乐特殊处理
          if (this.data.currentPage == 1 && item == 0) {
            this.setData({
              pageInfo: data.data,
              loading: false,
              singleList: data.data.musicList,
              singerList: data.data.userList
            })
          } else if (this.data.currentPage > 1 && item == 0) {
            const newSingleList = this.data.singleList
            for (let music of data.data.musicList.list) {
              newSingleList.list.push(music)
            }

            const newSingerList = this.data.singerList
            for (let singer of data.data.userList.list) {
              newSingerList.list.push(singer)
            }
            console.log(newSingleList)
            console.log(newSingerList)
            this.setData({
              loading: false,
              singleList: newSingleList,
              singerList: newSingerList
            })
          } else if (this.data.currentPage == 1 && item != 0) {
            this.setData({
              pageInfo: data.data,
              loading: false,
            })
          } else if (this.data.currentPage > 1 && item != 0) {
            const newPageInfo = this.data.pageInfo
            for (let info of data.data.list) {
              newPageInfo.list.push(info)
            }
            console.log(newPageInfo)
            this.setData({
              loading: false,
              pageInfo: newPageInfo,
            })
          }
          console.log('加载')
        },
        fail: res => {
          console.log(res)
          this.setData({
            loading: false,
          })
        }
      })
      wx.hideLoading()

    },
    // 触底事件
    scrollToLower() {
      console.log('触底了')
      console.log(this.data.item == 0);
      if (this.data.item == 0) {
        console.log('加载单曲');
        this.loadmusic()
      } else {
        this.loadOther()
      }

    },
    loadmusic: function () {
      if (!this.data.loading && this.data.currentPage < this.data.pageInfo.musicList.pages) {
        this.setData({
          loading: true,
          currentPage: this.data.currentPage + 1
        })
        this.search()
      }
    },
    loadOther: function () {
      if (!this.data.loading && this.data.currentPage < this.data.pageInfo.pages) {
        this.setData({
          loading: true,
          currentPage: this.data.currentPage + 1
        })
        this.search()
      }
    },

    // 去歌单页面
    toListPage: function (e) {
      console.log('触发跳转')
      console.log(e)
      this.triggerEvent('changeDataId', e.currentTarget.dataset.musiclistid)
      this.triggerEvent('changePage', 'list')
    },
    // 去搜索用户的个人中心
    toPersonCenter: function (e) {
      console.log(e)
      this.triggerEvent('changeUserId', e.currentTarget.dataset.userid)
      this.triggerEvent('changePage', 'personCenter')
    },

    update: function (e) {
      this.triggerEvent('update')
    },

    addToList: function (e) {
      const index = e.currentTarget.dataset.musicid
      app.pushMusicToList(this.data.singleList.list[index], this.data.singerList.list[index].name)
      this.update()
    },

    addToListAndPlay: function (e) {
      const index = e.currentTarget.dataset.musicid
      app.setMusicAndPlay(this.data.singleList.list[index], this.data.singerList.list[index].name)
      this.update()
    },

    // 与input实现双向数据绑定
    inputChange: function (e) {
      this.setData({
        keyword: e.detail.value
      })
    },

    // 切换搜索
    changeItem: function (e) {
      console.log(e.target.dataset.item)
      this.setData({
        item: e.target.dataset.item,
        currentPage: 1,
        firstLoad: true
      })
      this.search()
    },
    btnSearch: function () {
      this.setData({
        currentPage: 1,
        firstLoad: true
      })
      this.search()
    }
  },
  lifetimes: {
    attached: function () {
      console.log('进入搜索页面')
      this.setData({
        keyword: this.properties.keyword
      })
      console.log(this.data.keyword)
      this.search()
    },

  }
})